<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.shsh.safemanage.model.ProblemDepartStatisticItem"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.model.Role"%>
<%@page import="com.shsh.safemanage.util.Constants"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.action.ActionCreator"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.dao.UserDao"%>
<%@page import="com.shsh.safemanage.dao.RoleDao"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>

<%
{
ProblemDao theProblemDao = DaoCreator.createProblemDao();
RoleDao theRoleDao = DaoCreator.createRoleDao();
UserDao theUserDao = DaoCreator.createUserDao();
UserAction theUserAction = ActionCreator.createUserAction();

User theUser = (User) session.getAttribute(Constants.SESSION_USER);
Role theRole = theUserAction.getUserRole(theUser);

String dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
String dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);

if (Util.isNullOrEmpty(dateStart)) {
	dateStart = (String) session.getAttribute(Constants.SESSION_DATE_START);
}
if (Util.isNullOrEmpty(dateEnd)) {
	dateEnd = (String) session.getAttribute(Constants.SESSION_DATE_END);
}

if ((theRole.getRoleType() & (Constants.ROLE_TYPE_BUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG | Constants.ROLE_TYPE_SHEBEIFUBUZHANG 
	| Constants.ROLE_TYPE_ANQUANZONGJIAN)) > 0) {
	List<ProblemDepartStatisticItem> processedProblemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> writeProblemItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> closedProblemItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> processingProblemItemList = new ArrayList<ProblemDepartStatisticItem>();
	
	if (!Util.isNullOrEmpty(dateStart) && !Util.isNullOrEmpty(dateEnd)) {
		processedProblemList = theProblemDao.getUsersTransferedProblemCount(theUser.getDepartment1ID(), dateStart, dateEnd);
		writeProblemItemList = theProblemDao.getProblemDepartStatistics(theUser.getDepartment1ID(), dateStart, dateEnd, 0, 0);
		closedProblemItemList = theProblemDao.getProblemDepartStatistics(theUser.getDepartment1ID(), dateStart, dateEnd, 
				Constants.PROBLEM_STATUS_CLOSE, 0);
		processingProblemItemList = theProblemDao.getProblemDepartStatistics(theUser.getDepartment1ID(), dateStart, dateEnd, 
				Constants.PROBLEM_STATUS_OPEN, Constants.PROBLEM_STATUS_CHUZHANG);
	}
	
	List<ProblemDepartStatisticItem> banziItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> zuzhangItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> zhuangzhizhangItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> zuyuanItemList = new ArrayList<ProblemDepartStatisticItem>();
	List<ProblemDepartStatisticItem> yixianItemList = new ArrayList<ProblemDepartStatisticItem>();
	
	for (ProblemDepartStatisticItem item : processedProblemList) {
		if (item.getRoleType() >= Constants.ROLE_TYPE_ANQUANZONGJIAN) {
			banziItemList.add(item);
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZHUANGZHIZHANG) {
			zhuangzhizhangItemList.add(item);
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUZHANG) {
			zuzhangItemList.add(item);
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUYUAN) {
			zuyuanItemList.add(item);
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_YIXIANYUANGONG) {
			yixianItemList.add(item);
		}
	}
	
	for (ProblemDepartStatisticItem item : writeProblemItemList) {
		if (item.getRoleType() >= Constants.ROLE_TYPE_ANQUANZONGJIAN) {
			for (ProblemDepartStatisticItem banziItem : banziItemList) {
				if (banziItem.getWriteUserID().equals(item.getWriteUserID())) {
					banziItem.setWriteProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZHUANGZHIZHANG) {
			for (ProblemDepartStatisticItem zhuangzhizhangItem : zhuangzhizhangItemList) {
				if (zhuangzhizhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zhuangzhizhangItem.setWriteProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUZHANG) {
			for (ProblemDepartStatisticItem zuzhangItem : zuzhangItemList) {
				if (zuzhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuzhangItem.setWriteProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUYUAN) {
			for (ProblemDepartStatisticItem zuyuanItem : zuyuanItemList) {
				if (zuyuanItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuyuanItem.setWriteProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_YIXIANYUANGONG) {
			for (ProblemDepartStatisticItem yixianItem : yixianItemList) {
				if (yixianItem.getWriteUserID().equals(item.getWriteUserID())) {
					yixianItem.setWriteProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		}
	}
	
	for (ProblemDepartStatisticItem item : closedProblemItemList) {
		if (item.getRoleType() >= Constants.ROLE_TYPE_ANQUANZONGJIAN) {
			for (ProblemDepartStatisticItem banziItem : banziItemList) {
				if (banziItem.getWriteUserID().equals(item.getWriteUserID())) {
					banziItem.setClosedProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZHUANGZHIZHANG) {
			for (ProblemDepartStatisticItem zhuangzhizhangItem : zhuangzhizhangItemList) {
				if (zhuangzhizhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zhuangzhizhangItem.setClosedProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUZHANG) {
			for (ProblemDepartStatisticItem zuzhangItem : zuzhangItemList) {
				if (zuzhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuzhangItem.setClosedProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUYUAN) {
			for (ProblemDepartStatisticItem zuyuanItem : zuyuanItemList) {
				if (zuyuanItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuyuanItem.setClosedProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_YIXIANYUANGONG) {
			for (ProblemDepartStatisticItem yixianItem : yixianItemList) {
				if (yixianItem.getWriteUserID().equals(item.getWriteUserID())) {
					yixianItem.setClosedProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		}
	}
	
	for (ProblemDepartStatisticItem item : processingProblemItemList) {
		if (item.getRoleType() >= Constants.ROLE_TYPE_ANQUANZONGJIAN) {
			for (ProblemDepartStatisticItem banziItem : banziItemList) {
				if (banziItem.getWriteUserID().equals(item.getWriteUserID())) {
					banziItem.setProcessingProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZHUANGZHIZHANG) {
			for (ProblemDepartStatisticItem zhuangzhizhangItem : zhuangzhizhangItemList) {
				if (zhuangzhizhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zhuangzhizhangItem.setProcessingProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUZHANG) {
			for (ProblemDepartStatisticItem zuzhangItem : zuzhangItemList) {
				if (zuzhangItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuzhangItem.setProcessingProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_ZUYUAN) {
			for (ProblemDepartStatisticItem zuyuanItem : zuyuanItemList) {
				if (zuyuanItem.getWriteUserID().equals(item.getWriteUserID())) {
					zuyuanItem.setProcessingProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		} else if (item.getRoleType() >= Constants.ROLE_TYPE_YIXIANYUANGONG) {
			for (ProblemDepartStatisticItem yixianItem : yixianItemList) {
				if (yixianItem.getWriteUserID().equals(item.getWriteUserID())) {
					yixianItem.setProcessingProblemCount(item.getWriteProblemCount());
					break;
				}
			}
		}
	}
%>
<div style="font-weight: bold; font-size: 13pt" align="center">部门填报问题统计表</div>
<table align="center" border="1" width="70%" cellpadding="0" cellspacing="0" style="margin-top: 5px">
	<tr style="font-weight: bold" align="center">
		<td class="td1" colspan="2" width="25%">&nbsp;</td>
		<td class="td1" width="18%">填报问题</td>
		<td class="td1" width="18%">已整改</td>
		<td class="td1" width="18%">仍在整改</td>
		<td class="td1">查看</td>
	</tr>
	<%
	if (banziItemList.size() > 0) {
		ProblemDepartStatisticItem item = banziItemList.get(0);
	%>
	<tr align="center">
		<td rowspan="<%=banziItemList.size()%>">作业部班子成员</td>
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		for (int i = 1; i < banziItemList.size(); i++) {
			item = banziItemList.get(i);
	%>
	<tr align="center">
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		}
	}
	%>
	
	<%
	if (zuzhangItemList.size() > 0) {
		ProblemDepartStatisticItem item = zuzhangItemList.get(0);
	%>
	<tr align="center">
		<td rowspan="<%=zuzhangItemList.size()%>">组 长</td>
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		for (int i = 1; i < zuzhangItemList.size(); i++) {
			item = zuzhangItemList.get(i);
	%>
	<tr align="center">
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		}
	}
	%>
	
	<%
	if (zhuangzhizhangItemList.size() > 0) {
		ProblemDepartStatisticItem item = zhuangzhizhangItemList.get(0);
	%>
	<tr align="center">
		<td rowspan="<%=zhuangzhizhangItemList.size()%>">装置长</td>
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		for (int i = 1; i < zhuangzhizhangItemList.size(); i++) {
			item = zhuangzhizhangItemList.get(i);
	%>
	<tr align="center">
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		}
	}
	%>
	
	<%
	if (zuyuanItemList.size() > 0) {
		ProblemDepartStatisticItem item = zuyuanItemList.get(0);
	%>
	<tr align="center">
		<td rowspan="<%=zuyuanItemList.size()%>">组 员</td>
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		for (int i = 1; i < zuyuanItemList.size(); i++) {
			item = zuyuanItemList.get(i);
	%>
	<tr align="center">
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		}
	}
	%>
	
	<%
	if (yixianItemList.size() > 0) {
		ProblemDepartStatisticItem item = yixianItemList.get(0);
	%>
	<tr align="center">
		<td rowspan="<%=yixianItemList.size()%>">一线员工</td>
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		for (int i = 1; i < yixianItemList.size(); i++) {
			item = yixianItemList.get(i);
	%>
	<tr align="center">
		<td><%=item.getWriterName()%></td>
		<td><%=item.getWriteProblemCount()%></td>
		<td><%=item.getClosedProblemCount()%></td>
		<td><%=item.getProcessingProblemCount()%></td>
		<td><a href="javascript:" 
			onclick="showDialog('userTransferedProblemsDialog', '人员经办任务', 
					'user_transfered_problem_list_section.jsp?dateStart=<%=dateStart%>&dateEnd=<%=dateEnd%>&userID=<%=item.getWriteUserID()%>&name=<%=item.getWriterName()%>', 
					900, 500)" 
			style="color: blue">详细内容</a></td>
	</tr>
	<%
		}
	}
	%>
</table>
<%	
}
}
%>